package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSONArray;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.form.AttachmentForm;
import io.renren.modules.app.service.UserService;
import io.renren.modules.quickService.entity.OrderEntity;
import io.renren.modules.quickService.service.OrderOperationService;
import io.renren.modules.quickService.service.OrderService;
import io.renren.modules.quickService.vo.OrderOperate;
import io.renren.modules.quickService.vo.OrderStatistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单表
 *
 * @author zenger380
 * @email zenger380@163.com
 * @date 2018-09-19 17:05:51
 */

@RestController
@RequestMapping("app/order")
@Api(value = "APP订单管理类", tags = {"APP订单管理接口"})
public class AppOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    UserService appUserService;
    @Autowired
    OrderOperationService orderOperationService;

    /**
     * 信息
     */
    @Login
    @GetMapping("/info/{id}")
    @ApiOperation("查询订单")

    public R info(@PathVariable("id") Long id) {
        OrderEntity order = orderService.selectById(id);

        return R.ok().put("order", order);
    }

    /**
     * 接单
     */
    @Login
    @PostMapping("/take")
    @ApiOperation("接单")

    public R take(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.takeOrder(orderOperate, userId);
        if (!flag) {
            return R.error("接单失败");
        }
        return R.ok();
    }


    /**
     * 按订单状态分页查询
     */
    @Login
    @PostMapping("/orderList")
    @ApiOperation("按订单状态分页查询")

    public R orderList(@RequestBody Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 抢单
     */
    @Login
    @PostMapping("/contend")
    @ApiOperation("抢单")

    public R contend(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.contend(orderOperate, userId);
        if (!flag) {
            return R.error("抢单失败");
        }
        return R.ok();
    }


    /**
     * 拒单
     */
    @Login
    @PostMapping("/refuse")
    @ApiOperation("拒单")

    public R refuse(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.refuseOrder(orderOperate, userId);
        if (!flag) {
            return R.error("拒单失败");
        }
        return R.ok();
    }


    /**
     * 看现场
     */
    @Login
    @PostMapping("/visiting")
    @ApiOperation("看现场")

    public R visiting(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.visiting(orderOperate, userId);
        if (!flag) {
            return R.error("看现场记录失败");
        }
        return R.ok();
    }


    /**
     * 签协议
     */
    @Login
    @PostMapping("/agreement")
    @ApiOperation("签协议")

    public R agreement(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.agreement(orderOperate, userId);
        if (!flag) {
            return R.error("签协议失败");
        }
        return R.ok();
    }

    /**
     * 中期验收
     */
    @Login
    @PostMapping("/confirm")
    @ApiOperation("中期验收")

    public R confirm(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.confirm(orderOperate, userId);
        if (!flag) {
            return R.error("中期验收失败");
        }
        return R.ok();
    }

    /**
     * 中期验收
     */
    @Login
    @PostMapping("/complete")
    @ApiOperation("完成施工")

    public R complete(@RequestBody OrderOperate orderOperate, @RequestAttribute(name = "userId") long userId) {
        String attachmentList = orderOperate.getAttachmentList();
        List<AttachmentForm> attachmentForms = JSONArray.parseArray(attachmentList, AttachmentForm.class);
        orderOperate.setAttachmentListForm(attachmentForms);
        boolean flag = orderService.complete(orderOperate, userId);
        if (!flag) {
            return R.error("完工失败");
        }
        return R.ok();
    }


    /**
     * 订单状态统计
     */
    @Login
    @PostMapping("/statistics")
    @ApiOperation(" 订单状态统计")

    public R statistics(@RequestAttribute(name = "userId") long userId) {
        List<OrderStatistics> statistics = orderService.statistics(userId);
        return R.ok().put("statistics", statistics);
    }

    /**
     * 操作历史
     */
    @Login
    @PostMapping("/operateList")
    @ApiOperation(" 订单操作历史(分页)")
    public R operateList(@RequestBody Map<String, Object> params) {
        PageUtils page = orderOperationService.queryPage(params);
        return R.ok().put("page", page);
    }
}
